package com.nail.api.customMake.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.druid.util.StringUtils;
import com.nail.api.customMake.bean.PhoneModel;
import com.nail.api.customMake.service.IMailService;
import com.nail.api.user.bean.User;
import com.nail.api.user.service.IUserService;
import com.nail.client.RedisClient;
import com.nail.common.Constant;
import com.nail.controller.BaseController;
import com.nail.utils.IPUtils;
import com.nail.utils.PageResult;
import com.nail.utils.Result4API;

@Controller
@RequestMapping("/system/mail")
public class MailController  extends BaseController{
	Logger logger = Logger.getLogger(this.getClass());

	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

	@Autowired
	private RedisClient redisClient; 

	@Autowired
	private IMailService mailService;
	
	@Autowired
	private IUserService userService;

	@Value("${nail.host}")  
	private String host;  

	@Value("${nail.port}")  
	private String port;  

	@PostMapping(value="/sendSimpleMail")
	@ResponseBody
	public Result4API sendSimpleMail(HttpServletRequest request,String userName){
		Result4API result = new Result4API();
		logger.info(sdf.format(new Date()) +" "+ IPUtils.getRemoteHost(request) + ",用户访问了本接口：sendSimpleMail" );

		try {
			User user = userService.getUserByName(userName);
			if(user != null) {
				String email = user.getEmail();
				if(!StringUtils.isEmpty(email)) {
					String code = UUID.randomUUID().toString().replaceAll("-", "");
					String subject = "<html>"
							+ "<head></head>"
							+ "<body>"
							+ "<h1>"+ Constant.MAIL_TITLE + "</h1>"
							+ "<h3>"
							+ "<a href='"+ host +"/#/password?code=" + code + "'>"
							+ ""+ host +"/#/password?code=" + code + "</href>"
							+ "</h3>"
							+ "</body>"
							+ "</html>";
					redisClient.set(Constant.CHANGE_USER_KEY + code, user, 60L*10L);
					mailService.sendHtmlMail(email,Constant.MAIL_TITLE,subject);
					result.setCode(Constant.API_CODE_SUCCESS);
					result.setMsg(Constant.API_MSG_SUCCESS);
				}
			}else {
				result.setCode(Constant.API_CODE_1006);
				result.setMsg(Constant.API_MSG_1006);
			}
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.toString());
			result.setCode(Constant.API_CODE_ERROR);
			result.setMsg(Constant.API_MSG_ERROR);
			return result;
		}
	}
	
	@PostMapping(value="/changePWD")
	@ResponseBody
	public Result4API changePWD(HttpServletRequest request,String password,String code){
		Result4API result = new Result4API();
		logger.info(sdf.format(new Date()) +" "+ IPUtils.getRemoteHost(request) + ",用户访问了本接口：changePWD" );
		try {
			Object object = redisClient.get(Constant.CHANGE_USER_KEY+code);
			if(object != null) {
				User user = (User)object;
				user.setPassword(password);
				userService.updateUser(user);
				result.setCode(Constant.API_CODE_SUCCESS);
				result.setMsg(Constant.API_MSG_SUCCESS);
			}else {
				result.setCode(Constant.API_CODE_1006);
				result.setMsg(Constant.API_MSG_1006);
			}
			redisClient.remove(Constant.CHANGE_USER_KEY+code);
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.toString());
			result.setCode(Constant.API_CODE_ERROR);
			result.setMsg(Constant.API_MSG_ERROR);
			return result;
		}
	}
	

}
